namespace SourceFoundry.Utilities.BugManagement
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;

    public class HandledCrashManager
    {
        static HandledCrashManager()
        {
            HandledCrashManager._blnHaveException = false;
            HandledCrashManager._blnEmailError = true;
        }

        private static string ExceptionToMore(Exception objException)
        {
            StringBuilder builder1 = new StringBuilder();
            StringBuilder builder2 = builder1;
            if (HandledCrashManager._blnEmailError)
            {
                builder2.Append("Information about this problem was automatically mailed to ");
                builder2.Append(AppSettings.GetString("CrashManager/EmailTo"));
                builder2.Append(Environment.NewLine + Environment.NewLine);
            }
            builder2.Append("Detailed technical information follows: " + Environment.NewLine);
            builder2.Append("---" + Environment.NewLine);
            string text2 = CrashManager.ExceptionToString(objException);
            builder2.Append(text2);
            builder2 = null;
            return builder1.ToString();
        }

        private static string GetDefaultMore(string strMoreDetails)
        {
            if (Operators.CompareString(strMoreDetails, "", false) == 0)
            {
                StringBuilder builder1 = new StringBuilder();
                StringBuilder builder2 = builder1;
                builder2.Append("No further information is available. If the problem persists, contact (contact).");
                builder2.Append(Environment.NewLine);
                builder2.Append(Environment.NewLine);
                builder2.Append("Basic technical information follows: " + Environment.NewLine);
                builder2.Append("---" + Environment.NewLine);
                builder2.Append(CrashManager.SysInfoToString(true));
                builder2 = null;
                return builder1.ToString();
            }
            return strMoreDetails;
        }

        private static void ProcessStrings(ref string strWhatHappened, ref string strHowUserAffected, ref string strWhatUserCanDo, ref string strMoreDetails)
        {
            strWhatHappened = HandledCrashManager.ReplaceStringVals(strWhatHappened);
            strHowUserAffected = HandledCrashManager.ReplaceStringVals(strHowUserAffected);
            strWhatUserCanDo = HandledCrashManager.ReplaceStringVals(strWhatUserCanDo);
            strMoreDetails = HandledCrashManager.ReplaceStringVals(HandledCrashManager.GetDefaultMore(strMoreDetails));
        }

        private static string ReplaceStringVals(string strOutput)
        {
            string text2;
            if (strOutput == null)
            {
                text2 = "";
            }
            else
            {
                text2 = strOutput;
            }
            text2 = text2.Replace("(app)", AppSettings.AppProduct);
            return text2.Replace("(contact)", AppSettings.GetString("CrashManager/ContactInfo"));
        }

        private static void SendEmail(string strEmailBody)
        {
            HandledCrashManager._strEmailBody = strEmailBody;
            Thread thread1 = new Thread(new ThreadStart(HandledCrashManager.ThreadHandler));
            thread1.Name = "HandledExceptionEmail";
            thread1.Start();
        }

        private static void SendNotificationEmail(string strWhatHappened, string strHowUserAffected, string strWhatUserCanDo, string strMoreDetails)
        {
            //if (!CrashManager.IgnoreDebugErrors || !AppSettings.DebugMode)
            //{
            //    StringBuilder builder1 = new StringBuilder();
            //    StringBuilder builder2 = builder1;
            //    builder2.Append("What happened:");
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(strWhatHappened);
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append("How this will affect the user:");
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(strHowUserAffected);
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append("What the user can do about it:");
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(strWhatUserCanDo);
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append("More information:");
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(strMoreDetails);
            //    builder2.Append(Environment.NewLine);
            //    builder2.Append(Environment.NewLine);
            //    builder2 = null;
            //    HandledCrashManager.SendEmail(builder1.ToString());
            //}
        }

        public static DialogResult ShowDialog(string strWhatHappened, string strHowUserAffected, string strWhatUserCanDo)
        {
            HandledCrashManager._blnHaveException = false;
            return HandledCrashManager.ShowDialogInternal(strWhatHappened, strHowUserAffected, strWhatUserCanDo, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, UserErrorDefaultButton.Default);
        }

        public static DialogResult ShowDialog(string strWhatHappened, string strHowUserAffected, string strWhatUserCanDo, Exception objException, [Optional] MessageBoxButtons Buttons /* = 0 */, [Optional] MessageBoxIcon Icon /* = 0x30 */, [Optional] UserErrorDefaultButton DefaultButton /* = 0 */)
        {
            HandledCrashManager._blnHaveException = true;
            HandledCrashManager._strExceptionType = objException.GetType().FullName;
            return HandledCrashManager.ShowDialogInternal(strWhatHappened, strHowUserAffected, strWhatUserCanDo, HandledCrashManager.ExceptionToMore(objException), Buttons, Icon, DefaultButton);
        }

        public static DialogResult ShowDialog(string strWhatHappened, string strHowUserAffected, string strWhatUserCanDo, string strMoreDetails, [Optional] MessageBoxButtons Buttons /* = 0 */, [Optional] MessageBoxIcon Icon /* = 0x30 */, [Optional] UserErrorDefaultButton DefaultButton /* = 0 */)
        {
            HandledCrashManager._blnHaveException = false;
            return HandledCrashManager.ShowDialogInternal(strWhatHappened, strHowUserAffected, strWhatUserCanDo, strMoreDetails, Buttons, Icon, DefaultButton);
        }

        private static DialogResult ShowDialogInternal(string strWhatHappened, string strHowUserAffected, string strWhatUserCanDo, string strMoreDetails, MessageBoxButtons Buttons, MessageBoxIcon Icon, UserErrorDefaultButton DefaultButton)
        {
            HandledCrashManager.ProcessStrings(ref strWhatHappened, ref strHowUserAffected, ref strWhatUserCanDo, ref strMoreDetails);
            CrashDialog dialog1 = new CrashDialog();
            CrashDialog dialog2 = dialog1;
            CrashDialog dialog3 = dialog1;
            int num1 = (int) Buttons;
            switch (num1)
            {
                case 0:
                {
                    //dialog3.btn3.Text = "OK";
                    //dialog3.btn2.Visible = false;
                    //dialog3.btn1.Visible = false;
                    //dialog3.AcceptButton = dialog1.btn3;
                    break;
                }
                case 1:
                {
                    //dialog3.btn3.Text = "Cancel";
                    //dialog3.btn2.Text = "OK";
                    //dialog3.btn1.Visible = false;
                    //dialog3.AcceptButton = dialog1.btn2;
                    //dialog3.CancelButton = dialog1.btn3;
                    break;
                }
                case 2:
                {
                    //dialog3.btn1.Text = "&Abort";
                    //dialog3.btn2.Text = "&Retry";
                    //dialog3.btn3.Text = "&Ignore";
                    //dialog3.AcceptButton = dialog1.btn2;
                    //dialog3.CancelButton = dialog1.btn3;
                    break;
                }
                case 3:
                {
                    //dialog3.btn3.Text = "Cancel";
                    //dialog3.btn2.Text = "&No";
                    //dialog3.btn1.Text = "&Yes";
                    //dialog3.CancelButton = dialog1.btn3;
                    break;
                }
                case 4:
                {
                    //dialog3.btn3.Text = "&No";
                    //dialog3.btn2.Text = "&Yes";
                    //dialog3.btn1.Visible = false;
                    break;
                }
                case 5:
                {
                    //dialog3.btn3.Text = "Cancel";
                    //dialog3.btn2.Text = "&Retry";
                    //dialog3.btn1.Visible = false;
                    //dialog3.AcceptButton = dialog1.btn2;
                    //dialog3.CancelButton = dialog1.btn3;
                    break;
                }
            }
            dialog3 = null;
            MessageBoxIcon icon1 = Icon;
            if (icon1 == MessageBoxIcon.Hand)
            {
                //dialog1.PictureBox1.Image = SystemIcons.Error.ToBitmap();
            }
            else if (icon1 == MessageBoxIcon.Hand)
            {
                //dialog1.PictureBox1.Image = SystemIcons.Error.ToBitmap();
            }
            else if (icon1 == MessageBoxIcon.Exclamation)
            {
                //dialog1.PictureBox1.Image = SystemIcons.Exclamation.ToBitmap();
            }
            else if (icon1 == MessageBoxIcon.Asterisk)
            {
                //dialog1.PictureBox1.Image = SystemIcons.Information.ToBitmap();
            }
            else if (icon1 == MessageBoxIcon.Question)
            {
                //dialog1.PictureBox1.Image = SystemIcons.Question.ToBitmap();
            }
            else
            {
                //dialog1.PictureBox1.Image = SystemIcons.Error.ToBitmap();
            }
            num1 = ((int) DefaultButton) - 1;
            switch (num1)
            {
                case 0:
                {
                    //dialog1.AcceptButton = dialog1.btn1;
                    //dialog1.btn1.TabIndex = 0;
                    break;
                }
                case 1:
                {
                    //dialog1.AcceptButton = dialog1.btn2;
                    //dialog1.btn2.TabIndex = 0;
                    break;
                }
                case 2:
                {
                    //dialog1.AcceptButton = dialog1.btn3;
                    //dialog1.btn3.TabIndex = 0;
                    break;
                }
            }
            if (HandledCrashManager._blnEmailError)
            {
                HandledCrashManager.SendNotificationEmail(strWhatHappened, strHowUserAffected, strWhatUserCanDo, strMoreDetails);
            }
            return dialog1.ShowDialog();
        }

        private static void ThreadHandler()
        {
            //SimpleMail.SMTPClient client1 = new SimpleMail.SMTPClient();
            //SimpleMail.SMTPMailMessage message1 = new SimpleMail.SMTPMailMessage();
            //SimpleMail.SMTPMailMessage message2 = message1;
            //message2.To = AppSettings.GetString("CrashManager/EmailTo");
            //if (HandledCrashManager._blnHaveException)
            //{
            //    message2.Subject = "Handled Exception notification - " + HandledCrashManager._strExceptionType;
            //}
            //else
            //{
            //    message2.Subject = "HandledCrashManager notification";
            //}
            //message2.Body = HandledCrashManager._strEmailBody;
            //message2 = null;
            //try
            //{
            //    client1.SendMail(message1);
            //}
            //catch (Exception exception2)
            //{
            //    ProjectData.SetProjectError(exception2);
            //    Exception exception1 = exception2;
            //    Debug.WriteLine("** SMTP email failed to send!");
            //    Debug.WriteLine("** " + exception1.Message);
            //    ProjectData.ClearProjectError();
            //}
        }


        public static bool EmailError
        {
            get
            {
                return HandledCrashManager._blnEmailError;
            }
            set
            {
                HandledCrashManager._blnEmailError = value;
            }
        }


        private static bool _blnEmailError;
        private static bool _blnHaveException;
        private const string _strDefaultMore = "No further information is available. If the problem persists, contact (contact).";
        private static string _strEmailBody;
        private static string _strExceptionType;


        public enum UserErrorDefaultButton
        {
            Default,
            Button1,
            Button2,
            Button3
        }
    }
}

